package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.R;
import com.example.demo.common.RPage;
import com.example.demo.entity.allContents.*;
import com.example.demo.mapper.AllContentsMapper;
import com.example.demo.service.AllContentsService;
import com.example.demo.service.baseContext.BaseContextService;
import com.example.demo.utils.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class AllContentsServiceImpl extends ServiceImpl<AllContentsMapper, AllContents> implements AllContentsService {
    @Autowired
    private BaseContextService bcs;

    @Override
    public R<RPage<AllContents>> selAllContents(AllContentsSelList param) {
        LambdaQueryWrapper<AllContents> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtils.isValid(param.getTitleId()), AllContents::getTitleId, param.getTitleId())
                .like(ObjectUtils.isValid(param.getRole()), AllContents::getRole, param.getRole())
                .like(ObjectUtils.isValid(param.getContent()), AllContents::getContent, param.getContent())
                .eq(AllContents::getCreateBy, bcs.getUserData().getUserId())
                .orderByAsc(AllContents::getCreateTime);
        Page<AllContents> page = new Page<>(param.getPageNum(), param.getPageSize());
        Page<AllContents> page1 = baseMapper.selectPage(page, wrapper);
        RPage<AllContents> rPage = RPage.of(page1);
        return R.ok(rPage);
    }

    @Override
    public R<List<AllContents>> selAllAllContents(AllContentsSelAll param) {
        LambdaQueryWrapper<AllContents> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtils.isValid(param.getTitleId()), AllContents::getTitleId, param.getTitleId())
                .like(ObjectUtils.isValid(param.getRole()), AllContents::getRole, param.getRole())
                .like(ObjectUtils.isValid(param.getContent()), AllContents::getContent, param.getContent())
                .eq(AllContents::getCreateBy, bcs.getUserData().getUserId())
                .orderByAsc(AllContents::getCreateTime);
        List<AllContents> list = baseMapper.selectList(wrapper);
        return R.ok(list);
    }

    @Override
    public R<List<AllContents>> selOnesAllContents(Integer[] ids) {
        List<AllContents> list = baseMapper.selectByIds(Arrays.asList(ids));
        return R.ok(list);
    }

    @Override
    public R<AllContents> selOneAllContents(Integer id) {
        AllContents data = baseMapper.selectById(id);
        return R.ok(data);
    }

    @Override
    public R<Boolean> insAllContents(AllContentsInsOne allContentsInsOne) {
        AllContents allContents = new AllContents();
        BeanUtils.copyProperties(allContentsInsOne, allContents);
        baseMapper.insert(allContents);
        return R.ok(true);
    }

    @Override
    public R<Boolean> insAllContentss(AllContentsInsOne[] allContentsInsOnes) {
        for (AllContentsInsOne allContentsInsOne : allContentsInsOnes) {
            AllContents allContents = new AllContents();
            BeanUtils.copyProperties(allContentsInsOne, allContents);
            baseMapper.insert(allContents);
        }
        return R.ok(true);
    }

    @Override
    public R<Boolean> updAllContents(AllContentsUpdOne allContentsUpdOne) {
        AllContents allContents = new AllContents();
        BeanUtils.copyProperties(allContentsUpdOne, allContents);
        baseMapper.updateById(allContents);
        return R.ok(true);
    }

    @Override
    public R<Boolean> updAllContentss(AllContentsUpdOne[] allContentsUpdOnes) {
        for (AllContentsUpdOne allContentsUpdOne : allContentsUpdOnes) {
            AllContents allContents = new AllContents();
            BeanUtils.copyProperties(allContentsUpdOne, allContents);
            baseMapper.updateById(allContents);
        }
        return R.ok(true);
    }

    @Override
    public R<Boolean> delAllContents(Integer[] ids) {
        baseMapper.deleteByIds(Arrays.asList(ids));
        return R.ok(true);
    }
}
